import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/config/config.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:xvideos/hj_page/home_business/home_cell_style/video_type_menu_view.dart';
import 'package:xvideos/hj_page/home_business/tag/video_tag_logic.dart';
import 'package:xvideos/tools_base/appbar/base_app_bar.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';
import '../../../routers/jump_router.dart';
import '../../../tools_base/global_store/store.dart';

class VideoTagPage extends StatelessWidget {
  final TagsBean? model;
  final bool isShortStyle;
  final bool isHYStyle; // 黄油
  final bool isFromSearch;

  const VideoTagPage(
    this.model, {
    super.key,
    this.isShortStyle = false,
    this.isHYStyle = false,
    this.isFromSearch = false,
  });

  @override
  Widget build(BuildContext context) {
    return GetBuilder<VideoTagLogic>(
      init: VideoTagLogic(model, isShortStyle: isShortStyle),
      tag: "${DateTime.now().millisecondsSinceEpoch}",
      builder: (controller) {
        return Scaffold(
          appBar: BaseAppBar(title: controller.tagModel?.name ?? ''),
          body: Column(
            children: [
              Container(
                padding: EdgeInsets.only(left: 16),
                child: VideoTypeMenuView(
                  controller.sorts,
                  selectIndex: controller.sortIndex,
                  alignment: Alignment.center,
                  callback: (value) {
                    controller.sortIndex = value;
                    controller.fetchPageData(showLoading: true);
                  },
                ),
              ),
              Expanded(
                child: () {
                  if (controller.isLoading)
                    return LoadingCenterWidget();
                  else if (controller.dataSource.isEmpty)
                    return CErrorWidget();
                  else
                    return pullYsRefresh(
                      onInit: (ctr) => controller.refreshController = ctr,
                      onRefresh: (_) => controller.fetchPageData(),
                      onLoading: (_) => controller.fetchPageData(isRefresh: false),
                      child: GridView.builder(
                        padding: EdgeInsets.symmetric(horizontal: 16),
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisSpacing: 6,
                          mainAxisSpacing: 12,
                          crossAxisCount:  2,
                          childAspectRatio: isShortStyle ? 168 / 266 : 168 / 154,
                        ),
                        itemCount: controller.dataSource.length,
                        itemBuilder: (BuildContext context, int index) {
                          VideoModel model = controller.dataSource[index];
                          return VideoSimpleCellWidget(
                            videoModel: model,
                            textLines: (isShortStyle || isHYStyle) ? 1 : 2,
                            isFromHY: isHYStyle,
                            isFromSearch: isFromSearch,
                            onTap: () {
                              if (model.isDarkTag && !globalStore.isAWVIP()) {
                                jumpToWalletPage(vipId: Config.darkWebVipId);
                              } else {
                                pushToVideoPage(videoModel: model, videoArr: controller.dataSource);
                              }
                            },
                          );
                        },
                      ),
                    );
                }(),
              ),
            ],
          ),
        );
      },
    );
  }
}
